# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

source "testlib.tcl"

test rmdir_outside_sandbox "Test that rmdir(2) does not work outside of the sandbox" \
    -setup {
        file mkdir rmdirme
        [setup [list deny /]]
    } \
    -cleanup {
        file delete -force rmdirme
        [expect [list "$cwd/rmdirme"]]
    } \
    -body {exec -ignorestderr -- ./rmdir rmdirme 2>@1} \
    -result "rmdir: No such file or directory"

test rmdir_inside_sandbox "Test that rmdir(2) works inside the sandbox" \
    -setup {
        file mkdir rmdirme
        [setup [list allow /]]
    } \
    -cleanup {
        file delete -force rmdirme
        [expect {}]
    } \
    -body {
        exec -ignorestderr -- ./rmdir rmdirme 2>@1
        return [file isdirectory rmdirme]
    } \
    -result 0

test rmdir_uninitialized "Test that rmdir(2) works outside the sandbox if darwintrace is uninitialized" \
    -setup {
        file mkdir rmdirme
        [setup [list deny /]]
    } \
    -cleanup {
        file delete -force rmdirme
        [expect {}]
    } \
    -body {
        set ::env(DARWINTRACE_UNINITIALIZE) 1
        exec -ignorestderr -- ./rmdir rmdirme 2>@1
        unset ::env(DARWINTRACE_UNINITIALIZE)
        return [file isdirectory rmdirme]
    } \
    -result 0

cleanupTests
